<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/3/18 0018
 * Time: 22:33
 */

namespace app\admin\controller;
use think\Db;
use app\admin\model\AuthGroup as AuthGroupModel;
use think\migration\command\migrate\Status;

class AuthGroup extends Common
{
    public function lst(){
        $auth_group=new AuthGroupModel();
        $res=$auth_group->paginate(2);
        $this->assign('res',$res);
        return view();
    }
    public function add(){
        $rule=new \app\admin\model\AuthRule();
        $rules=$rule->sort();
        $this->assign('rule',$rules);
        if(request()->isPost()){
            $data=input('post.');
            $data['rules'] = implode(",", $data['rules']);
            $add=\db('auth_group')->insert($data);
            if($add){
                $this->success("添加成功",'lst');
            }else{
                $this->error('添加失败','add');
            }
        }
        return view();
    }
    public function edit(){
        $rule=new \app\admin\model\AuthRule();
        $rules=$rule->sort();
        $this->assign('rule',$rules); //获取规则列表
        $id=input('id');
        $auth_group=new AuthGroupModel();
        $res=$auth_group->find($id);
        $res['rules']=explode(",", $res['rules']);
        $this->assign('res',$res);//获取角色列表
        if(request()->isPost()){
            $data=input('post.');
            $_data=array();
            foreach ($data  as $k=>$v){
                $_data[]=$k;
            }
            if(!in_array('status',$_data)){
                $data['status']=0;
            }
            $data['rules'] = implode(",", $data['rules']);
            $chg=$auth_group->update($data);
            if($chg){
                $this->success("修改成功",'lst');
            }
            else{
                $this->error('修改失败');
            }
        }
        return view();
    }
    public function del(){
        $id=input('id');
        $auth_group=new AuthGroupModel();
        if($auth_group->destroy($id)){
            return $this->success("删除成功",'lst');
        }else{
            return $this->error('删除失败','lst');
        }
    }
}